/*
 * Copyright (c) 2012 Greg Henle ( http://code.google.com/p/jquery-selectsubstr/ )
 * Licensed under GNU GPL v3: http://www.gnu.org/licenses/gpl.html
 */
(function($){
	$.fn.selectSubStr = function(o) {
		var txtSel = false
		var opt = $.extend({
			start: 0,
			end: false
		}, o);
		var allowedTags = [
			'input',
			'textarea'
		];

		if(typeof opt.start != 'number')
			opt.start = 0;
		
		// we'll pass through all matches but only
		// process the first of the allowed tag types.
		return this.each(function(){
			var tag = this.nodeName.toLowerCase();
			if( txtSel || $.inArray(tag, allowedTags) == -1 ){
				return true;
			};

			if(typeof opt.end != 'number'){
				opt.end = $(this).val().length;
			};

			if( this.createTextRange ) {
				var selRange = this.createTextRange();
				selRange.collapse(true);
				selRange.moveStart('character', opt.start);
				selRange.moveEnd('character', opt.end);
				selRange.select();
			} else if( this.setSelectionRange ) {
				this.setSelectionRange(opt.start, opt.end);
			} else if( this.selectionStart ) {
				this.selectionStart = opt.start;
				this.selectionEnd = opt.end;
			};
			
			$(this).focus();
			txtSel = true;
		});
	}
})(jQuery);